package com.cjz.servlet;

import com.cjz.dao.DButil;
import com.cjz.domain.User;
import org.codehaus.jackson.map.ObjectMapper;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "AddCourseServlet",urlPatterns = {"/AddCourseServlet"})
public class AddCourseServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        //获取用户名
        String userName =((User) request.getSession().getAttribute("user")).getUserName();
        PrintWriter out = response.getWriter();
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> msgs = new HashMap<>();
        //与数据库进行连接
        DButil db = new DButil();
        db.getConn();
        //获取选课ID
        String[] courseValues = (request.getParameter("value")).split(",");
        //提示已经选择的课程信息
        StringBuilder isSelectedMessage = new StringBuilder();
        //获取已经选择的课程信息
        List<String> isSelectedCoursesList = db.getIsSelectedCourse(userName, courseValues);
        try {
            if (isSelectedCoursesList.size() > 0) {
                for (String isSelectedCourseName : isSelectedCoursesList) {
                    isSelectedMessage.append("《").append(isSelectedCourseName).append("》");
                }
                isSelectedMessage.append("课程您已选，不可重复选择！");
                msgs.put("msg", isSelectedMessage);
                out.print(mapper.writeValueAsString(msgs));
            }
            else {
                // 学生选课，selectCourse()返回的是没有容量的课程名List
                List<String> failCoursesList = db.selectCourse(userName, courseValues);
                if (failCoursesList.size() > 0) {
                    StringBuilder isFullMessage = new StringBuilder();
                    for (String failCourseName : failCoursesList) {
                        isFullMessage.append("《").append(failCourseName).append("》");
                    }
                    isFullMessage.insert(0, "课程名为：").append("的课程已经被选完！");
                    msgs.put("msg", isFullMessage);
                    out.print(mapper.writeValueAsString(msgs));
                }
                else {
                    StringBuilder isSuccessMessage = new StringBuilder("选课成功!");
                    msgs.put("msg", isSuccessMessage);
                    out.print(mapper.writeValueAsString(msgs));
                }
            }
        }
        catch (Exception e) {
            StringBuilder isEmptyMessage = new StringBuilder("请勾选您要选的课！!");
            msgs.put("msg", isEmptyMessage);
            out.print(mapper.writeValueAsString(msgs));
        }finally {
            db.release();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws  IOException {
        this.doPost(request, response);
    }
}
